package view;

import controller.MyController;
import entity.Score;
import service.UserOpt;

import javax.swing.*;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.List;


public class UserScoreShow extends JFrame implements ActionListener {
    String[] columnName = {"序号","考试科目","考试时间","考试成绩"};
    JTable table = null;
    JScrollPane scrollPane = null;
    String name;
    JLabel label,label2;
    JButton btn;
    JPanel panel1,panel2,panel3;
    public UserScoreShow(String name) throws IOException {
        super("历史成绩");
        ImageIcon icon = new ImageIcon("src\\main\\resources\\img\\sc.png");
        this.setIconImage(icon.getImage());
        this.setLayout(new BorderLayout());
        this.name = name;
        label = new JLabel(name+"的历史成绩");
        btn = new JButton("确认");
        panel1 = new JPanel();
        panel1.setLayout(new FlowLayout());
        panel3 = new JPanel();
        panel3.setLayout(new FlowLayout());
        panel1.add(label);
        panel3.add(btn);
        btn.addActionListener(this);
        panel1.setBackground(Color.ORANGE);
        if (MyController.getUserScore(name) != null){
            List<Score> scores = MyController.getUserScore(name);
            int n = scores.size();
            String[][] k = new String[n][4];
            for (int i =0;i<n;i++){
                Score score = scores.get(i);
                k[i][0] = String.valueOf(i+1);
                k[i][1] = score.getTestsName();
                k[i][2] = score.getDate();
                k[i][3] = score.getScore().toString();
            }
            table = new JTable(k,this.columnName);
            scrollPane = new JScrollPane(table);
            this.add(scrollPane,BorderLayout.CENTER);
        }else {
            label2 = new JLabel("成绩未查询到");
            panel2 = new JPanel();
            panel2.setLayout(new FlowLayout());
            panel2.add(label2);
            this.add(panel2,BorderLayout.CENTER);
        }
        this.add(panel1,BorderLayout.NORTH);
        this.add(panel3,BorderLayout.SOUTH);
        this.setVisible(true);
        this.setBounds(500,250,500,300);
    }

    public static void main(String[] args) throws IOException {
        new UserScoreShow("w");
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == btn){
            this.dispose();
        }
    }
}
